using System.Collections;
using System.Collections.Generic;
using Hotran.SaveSystem;
using Hotran.UI;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
    [SerializeField]
    private SceneTransition _sceneTransiton;
    [SerializeField]
    private SaveManager _saveManager;


    public AsyncOperation LoadSceneAsync(int sceneIndex)
    {
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneIndex);
        asyncOperation.allowSceneActivation = false;
        return asyncOperation;
    }

    public void ActivateAsyncScene(AsyncOperation asyncOperation)
    {
        _sceneTransiton.FadeOut(() => asyncOperation.allowSceneActivation = true);
    }

    public void ReloadScene()
    {
        _sceneTransiton.FadeOut(LoadCurrentScene);
    }

    public void GameOver()
    {
        _sceneTransiton.FadeOut(() =>
        {
            if (_saveManager.TryLoadCurrent())
            {
                _sceneTransiton.FadeIn();
            }
            else
            {
                LoadCurrentScene();
            }
        });
    }

    private void LoadCurrentScene() => SceneManager.LoadScene(
            gameObject.scene.buildIndex);
}
